package com.study.designpartten.single;

public class Singleton1 {

    private Singleton1(){}

    //1.memory = allocate() 分配内存空间
    //2.ctorInstance() 初始化对象
    //3.instance = memory 设置instance 指向刚分配的内存
//用volatile限制指令重排使其最终线程安全
    private volatile static Singleton1 intance = null;

    public static Singleton1 getIntance(){
        if(intance!=null){
            synchronized (Singleton1.class){
                if(intance!=null){
                    intance = new Singleton1();
                }
            }
        }
        return intance;
    }

    public static void main(String[] args) {
        Singleton1 s1 = Singleton1.getIntance();
        Singleton1 s2 = Singleton1.getIntance();
        System.out.println(s1==s2);
    }

}
